package com.linewow.xhyy.nihaonew.helper;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;

import com.linewow.xhyy.nihaonew.R;
import com.linewow.xhyy.nihaonew.event.MessageEvent;
import com.linewow.xhyy.nihaonew.inter.RecorderInter;
import com.linewow.xhyy.nihaonew.view.VoiceMenu;

import org.simple.eventbus.EventBus;

import java.io.File;


/**
 * Created by LXR on 2016/9/21.
 */
public class VoiceMenuHelper {
    private VoiceMenu popupView;
    private Context context;
    private View originView;
    private PopupWindow popupWindow;
    private String TAG="VoiceMenuHelper";
    private String storePath="null";

    public VoiceMenuHelper(Context context, View view, final String receiveTAG) {
        popupView=new  VoiceMenu(context);
        popupView.setPath("test");
        popupView.setPathSure(new RecorderInter() {
            @Override
            public void pathFinish(boolean flag) {
                Log.e(TAG,""+flag);
                if(flag){
                    EventBus.getDefault().post(new MessageEvent(storePath),receiveTAG);//点击了 提交
                    popupWindow.dismiss();
                    popupView.reset();

                }else{
                    storePath="null";//点击了重录  不需要发送  也不需要变为null
                }

            }

            @Override
            public void commitFinish(String path) {
                Log.e(TAG,"路径--"+path);
                storePath=path;
            }


        });
        popupView.setSwitchFlag(true);
        originView=view;
        init();
    }

    public void init(){
        popupWindow=new PopupWindow(popupView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        popupWindow.setFocusable(true);
        popupWindow.setAnimationStyle(R.style.anim_pop_bottom);
        ColorDrawable drawable=new ColorDrawable(0xb0000000);
        popupWindow.setBackgroundDrawable(drawable);
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {

            }
        });
    }

    public void show(){
        popupWindow.showAtLocation(originView, Gravity.BOTTOM,0,0);
    }

    public boolean delVoiceFile(){
        boolean flag=new File(storePath).delete();
        return  flag;
    }

}
